<?php
/**
 * Created by PhpStorm.
 * User: zhanglin02
 * Date: 2016/4/10
 * Time: 19:11
 */

namespace Admin\Model;


use Think\Model;

class BlogModel extends Model
{
    public function getList($type = 'blog',$cids = 0,$p = 0)
    {
        //准备查询条件
        $order = 'g.id desc'; //排序条件
        $field = 'c.name as category_name , g.category_id, g.id, g.name, g.show';
        if ($type == 'blog') {
            $where = array('g.recycle' => '0');
        } elseif ($type = 'recycle') {
            $where = array('g.recycle' => '1');
        }
        //cids = 0查找未分类博文,cids > 0 查找分类ID数组博文，cids < 0 查找全部博文
        if ($cids == 0) {//查找未分类波分
            $where['g.category_id'] = 0;
        } elseif ($cids > 0) {
            $where['g.category_id'] = array('in', $cids);
        }
        //准备分页查询
        $pagesize = C('USER_CONFIG.pagesize');//每页显示商品数
        $count = $this->alias('g')->where($where)->count();
        $Page = new \Think\Page($count, $pagesize);//实例化分页类
        $this->_customPage($Page);
        //查询数据
        $data = $this->alias('g')->join('__CATEGORY__ AS c ON c.id =g.category_id', 'LEFT')->field($field)->where($where)->order($order)->page($p, $pagesize)->select();
        //返回结果
        return array(
            'data' => $data,
            'pagelist' => $Page->show(),
        );
    }
    private function _customPage($Page){
        $Page->lastSuffix = false;
        $Page->setConfig('prev','上一页');
        $Page->setConfig('prev','下一页');
        $Page->setConfig('prev','首页');
        $Page->setConfig('prev','尾页');
    }
}